// 模块化
import Vue from 'vue'
import VueRouter from 'vue-router'
// 明确的安装路由功能
Vue.use(VueRouter)

// 创建路由模块和配置路由
const router = new VueRouter({

  routes: [
    {
      //新加一个默认首页
      name: 'default',
      path: '/',
      redirect: { name: 'login' }
    },
    {
      name: 'login',
      path: '/login',
      component: () => import('@/views/login.vue'),
    },
    {
      name: 'index',
      path: '/index',
      component: () => import('@/views/index.vue'),
      // 添加重定向，一进入到index就渲染hello
      redirect: { name: 'hello' },
      children: [
        {
          name: 'hello',
          path: 'hello',
          component: () => import('@/views/hello.vue')
        },
        {
          name: 'postlist',
          path: 'postlist',
          component: () => import('@/views/postlist.vue')
        },
        {
          name: 'postPublish',
          path: 'postPublish',
          component: () => import('@/views/postPublish.vue')
        },
      ]
    },

  ]

})

//添加导航守卫
router.beforeEach((to, from, next) => {
  if (to.path == "/login") {
    next()
  } else {
    if (localStorage.getItem('heima_back_token_59')) {
      next()

    } else {
      next({ name: 'login' })
    }
  }
})




//暴露
export default router